package com.example.bdmanagerapplication

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import com.blankj.utilcode.util.AppUtils

object MapUtil {
    private fun isAppInstalled(packageName: String,context: Context): Boolean {
        val pm = context.packageManager
        return try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
            true
        } catch (e: PackageManager.NameNotFoundException) {
            false
        }
    }
    fun checkMapApps():List<String> {
        val dialogList=ArrayList<String>()
        val amapInstalled = AppUtils.isAppInstalled("com.autonavi.minimap") // 高德地图包名
        val tmapInstalled = AppUtils.isAppInstalled("com.tencent.map") // 腾讯地图包名
        val bmapInstalled = AppUtils.isAppInstalled("com.baidu.BaiduMap") // 百度地图包名

        if (amapInstalled) {
            // 高德地图已安装
            // 执行相关逻辑
            dialogList.add("高德地图")
        }

        if (tmapInstalled) {
            // 腾讯地图已安装
            // 执行相关逻辑
            dialogList.add("腾讯地图")
        }

        if (bmapInstalled) {
            // 百度地图已安装
            // 执行相关逻辑
            dialogList.add("百度地图")
        }
        return dialogList
    }
    fun launchAMap(context: Context, latitude: Double, longitude: Double,dname:String) {
        val i1 = Intent()
        i1.data =
            Uri.parse("amapuri://route/plan/?sid=&slat=&slon=&sname=&did=&dlat=${latitude}&dlon=${longitude}&dname=${dname}&dev=0&t=0")
        context.startActivity(i1)
    }

    fun launchBaiduMap(context: Context, latitude: Double, longitude: Double,dname:String) {
        val i1 = Intent()
        i1.data =
            Uri.parse("baidumap://map/direction?origin=我的位置&destination=name:${dname}|latlng:${latitude},${longitude}&coord_type=bd09ll&src=andr.baidu.openAPIdemo")
        context.startActivity(i1)
    }

    fun launchTencentMap(context: Context, latitude: Double, longitude: Double,dname:String) {
        val i1 = Intent()
        i1.data =
            Uri.parse("qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&to=${dname}&tocoord=${latitude},${longitude}&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77")
        context.startActivity(i1)
    }
}